﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DotSpatial.Map;
using System.Windows.Forms;
using DotSpatial .Main ;
using DotSpatial .Drawing ;


using DotSpatial.Data;

using DotSpatial.Geometries;

namespace HuanGis
{
    /// <summary>
    /// 用于所有的层相关的管理
    /// </summary>
    [Serializable ]
   public  class LayerSystem:IFileSer 
    {

        public LayerSystem(FLayerManager p)
        {
            this.Parent = p;
 
        }



        public event LayerAddHandler LayerAdded;


       Dictionary<string, FLayer> m_Layers = new Dictionary<string, FLayer>();


       FLayerManager Parent;



       public void AddLayer(FLayer layer)
       {
           layer.ParentManger = this.Parent ;

           m_Layers.Add(layer.Id, layer);


           layer.BuildEntity();


           if (LayerAdded != null)
           {
               LayerAdded(layer);
           }



       }


       public void RemoveLayer(string id)
       {


       }


       public void RemoveAllLayer()
       {

       }



       public FLayer GetLayer(string lname)
       {

           if (m_Layers.ContainsKey(lname))
           {

               return m_Layers[lname];
           }
           else
           {

               foreach (FLayer item in m_Layers.Values)
               {
                   FCompoundLayer fcl = item as FCompoundLayer;
                   if (fcl != null)
                   {
                       if (fcl.IsChildExist(lname))
                       {
                           return fcl.GetChild(lname);
                       }

                   }

               }
               return null;
           }

       }



       public IFeatureLayer GetFeatureLayer(string lname)
       {

           FLayer flayer = this.GetLayer(lname);

           if (flayer != null)
           {
               FShapeLayer f = flayer as FShapeLayer;

               if (f != null)
               {
                   return f.IFLayer;

               }
               else
               {
                   return null;
               }

           }
           else
           {
               return null;
           }

       }

        /// <summary>
        /// 返回所有的可供编辑的图层featurelayer
        /// </summary>
        /// <returns></returns>
       public List<IFeatureLayer> GetEditableFeatureLayers()
       {
           List<IFeatureLayer> layers = new List<IFeatureLayer>();

           foreach (FLayer  item in m_Layers.Values  )
           {
               if(item .BShapeEditable )
               {
                   IFeatureLayer ifl = item.ILayer as IFeatureLayer;
                   layers.Add(ifl);

               }
               

           }



           return layers;

 
       }


       public List<FLayer> GetEditableLayers()
       {

           List<FLayer> layers = new List<FLayer>();


           foreach (FLayer item in m_Layers.Values)
           {
               if (item.BShapeEditable)
               {
                  
                   layers.Add(item );

               }


           }



           return layers;


       }



       #region  关于shape的辅助函数


        /// <summary>
        /// 添加图形到指定的图层，其中图形本身必须包含有对应的父亲定义
        /// </summary>
        /// <param name="shp"></param>
       public void AddShape(FShape shp)
       {

           string ln = shp.LayerName;
           if (ln!="")
           {

               FEditableLayer lay = (FEditableLayer)this.GetLayer(ln);
               if (lay!=null )
               {
                   lay.AddShape(shp);
                   
               }
               
           }

 
       }
       #endregion




       #region IFileSer 成员

       public bool SaveProj()
       {
           return true;
       }

       public bool OpenProj(string pfName)
       {
           return true;
       }

       public void NewProj(string pfName)
       {
           
       }

       public bool SaveAsProj(string pfName)
       {
           return true;
       }

       #endregion
    }
}
